package com.tgb.web;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  

import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
  

import com.tgb.entity.User;  
import com.tgb.manager.UserManager;  
  
@Controller 
/*
 * 由于是统一的路径，所以可以放在开头
 */
@RequestMapping("/user")  
public class UserController {  
	
   /*
	* 进入管理系统，并且在tiles框架下展现welcome的页面	
 	*/
	@RequestMapping("/welcome")
	 public String welcome()
	 {
	  return "welcome";
	 }
	@RequestMapping("/hello")
	 public String hello()
	 {
	  return "hello";
	 }
	@RequestMapping("/login")
	 public String login()
	 {
	  return "/login";
	 }
	/*
	 * 查询功能	
	 */
    @Resource(name="userManager")  
    private UserManager userManager;  
  
    @RequestMapping("/getAllUser")  
    public String getAllUser(HttpServletRequest request){  
          
        request.setAttribute("userList", userManager.getAllUser());  
          
        return "index";  //返回index的界面
      
    }  
    /*
     * 进行修改功能
     */
        
    @RequestMapping("/getUser")  
    public String getUser(String id,HttpServletRequest request){  
          
        request.setAttribute("user", userManager.getUser(id));  
      
        return "editUser";  
    }  
    /*
     * editUser发出请求，在这里进行处理，再把编辑的结果返回给/user/getAllUser
     */
    @RequestMapping("/updateUser")  
    public String updateUser(User user,HttpServletRequest request){  
          
        if(userManager.updateUser(user)){  
            user = userManager.getUser(user.getId());  
            request.setAttribute("user", user);  
            return "redirect:/user/getAllUser";  
        }else{  
            return "/error";  
        }  
  
    }
   
    /*
     * 进行删除功能
     */          
    @RequestMapping("/delUser")  
    public void delUser(String id,HttpServletResponse response){  
          
        String result = "{\"result\":\"error\"}";  
          
        if(userManager.delUser(id)){  
            result = "{\"result\":\"success\"}";  
        }  
          
        response.setContentType("application/json");  
          
        try {  
            PrintWriter out = response.getWriter();  
            out.write(result);  
        } catch (IOException e) {  
        	
            e.printStackTrace();  
        }  
    }  
      
   
    /*
     * 进行添加功能，进入添加界面
     */   
    @RequestMapping("/toAddUser")  
    public String toAddUser(){  
        return "addUser";  
    }  
    /*
     * 添加数据，结果返回给/user/getAllUser
     */    
    @RequestMapping("/addUser")  
    public String addUser(User user,HttpServletRequest request){  
          
        userManager.addUser(user);  
          
        return "redirect:/user/getAllUser";  
    }  
      
}  
